#!/bin/bash

#打包docker镜像
if [ ! -d "/usr/java/dj-cloud/log" ];then
  mkdir -p /usr/java/log/dj-cloud/
else
  echo "日志路径/usr/java/dj-cloud/log"
fi

echo "-------开始构建docker镜像-------"

echo "-------停止容器-------"
cid=$(docker ps --no-trunc|grep dj-cloud|grep 8090|awk '{print $1}')
echo $cid
if [ ! $cid ];then
    echo "容器未运行"
else
   echo "----删除旧的Container----"
   docker stop $cid
   docker rm $cid
fi
echo "-------停止容器success-------"

#删除历史Container
eid=$(docker ps -a --no-trunc|grep dj-cloud|grep 8090|awk '{print $1}')
for item in ${eid[*]}
do
 echo $item
 echo "----删除旧的Container----"
 docker rm $item
done

echo "-------删除镜像--------"
#删除镜像
iid=$(docker images|grep dj-cloud|awk '{print $3}')
echo $iid
if [ ! $iid ];then
    echo "镜像不存在"
else
   docker rmi $iid
fi
echo "-------删除镜像success--------"

echo "-------新镜像生成--------"
docker build -t dj-cloud .

echo "-------容器启动--------"
docker run --name=dj-cloud -d -p 8090:8090 -v /usr/java/dj-cloud/log:/logs dj-cloud
echo "-------启动成功--------"

echo "日志路径/usr/java/dj-cloud/log"

echo "-------启动后验证--curl http://localhost:8090/doc.html------"


